<template>
  <div class="app-container">
    <el-tabs type="border-card">
      <el-tab-pane label="基础配置">
        <div class="spaceline" />
        <el-form
          ref="form"
          v-loading="infoLoading"
          class="common-form"
          :model="form"
          label-width="120px"
        >
          <el-form-item label="appid">
            <el-input v-model="form.wechat_miniprogram_appid" class="middle" />
          </el-form-item>
          <el-form-item label="appsecret">
            <el-input v-model="form.wechat_miniprogram_appsecret" class="middle" />
          </el-form-item>
          <el-form-item label="小程序码">
            <el-upload
              class="qrcode-uploader"
              :action="apiUpload"
              :headers="headers"
              :show-file-list="false"
              :on-success="handleQrcodeSuccess"
              :before-upload="beforeUpload"
            >
              <img
                v-if="qrcodeUrl"
                :src="qrcodeUrl"
                class="qrcode"
              >
              <i
                v-else
                class="el-icon-plus qrcode-uploader-icon"
              />
            </el-upload>
            <span class="smalltip">
              <i class="el-icon-info" />
              建议尺寸430*430
            </span>
            <el-button v-if="qrcodeUrl!=''" type="text" class="img-del" @click="qrcodeUrl='';form.wechat_miniprogram_qrcode=''">删除</el-button>
          </el-form-item>
          <el-form-item label="" style="margin-top:36px;">
            <el-button type="primary" @click="onSubmit('form')">保存</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>

  </div>
</template>

<script>
import { setConfig } from '@/api/configuration'
import apiArr from '@/api'
import { getToken } from '@/utils/auth'

export default {
  data() {
    return {
      infoLoading: true,
      headers: { admintoken: getToken() },
      fileupload_size: '',
      fileupload_ext: '',
      apiUpload: window.global.RequestBaseUrl + apiArr.upload,
      qrcodeUrl: '',
      form: {
        wechat_miniprogram_appid: '',
        wechat_miniprogram_appsecret: '',
        wechat_miniprogram_qrcode: ''
      }
    }
  },
  computed: {
    config() {
      return this.$store.state.config
    }
  },
  created() {
    this.fileupload_size = this.config.fileupload_size
    this.fileupload_ext = this.config.fileupload_ext
    this.fetchInfo()
  },
  methods: {
    fetchInfo() {
      this.infoLoading = true
      const param = {}
      setConfig(param, 'get')
        .then(response => {
          const {
            wechat_miniprogram_appid,
            wechat_miniprogram_appsecret,
            wechat_miniprogram_qrcode,
            wechatMiniprogramQrcodeUrl
          } = { ...response.data }
          this.form = {
            wechat_miniprogram_appid,
            wechat_miniprogram_appsecret,
            wechat_miniprogram_qrcode
          }
          this.qrcodeUrl = wechatMiniprogramQrcodeUrl
          this.infoLoading = false
        })
        .catch(() => {})
    },
    onSubmit(formName) {
      const insertData = { ...this.form }
      this.$refs[formName].validate(valid => {
        if (valid) {
          setConfig(insertData)
            .then(response => {
              this.$message.success(response.message)
              return true
            })
            .catch(() => {})
        } else {
          return false
        }
      })
    },
    handleQrcodeSuccess(res, file) {
      if (res.code === 200) {
        this.qrcodeUrl = URL.createObjectURL(file.raw)
        this.form.wechat_miniprogram_qrcode = res.data.file_id
      } else {
        this.$message.error(res.message)
        return false
      }
    },
    beforeUpload(file) {
      const filetypeArr = file.type.split('/')
      const filetype = filetypeArr[1]
      const configFileExtArr = this.fileupload_ext.split(',')

      if (!configFileExtArr.includes(filetype)) {
        this.$message.error('上传文件格式不允许')
        return false
      }
      if (file.size / 1024 > this.fileupload_size) {
        this.$message.error(`上传文件最大为${this.fileupload_size}kb`)
        return false
      }
      return true
    }
  }
}
</script>
<style scoped>
.qrcode-uploader {
  display: inline-block;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  width: 122px;
  height: 122px;
  position:relative;
}
.img-del{
  position:absolute;
  left:45px;
  bottom:-20px;
}
.qrcode-uploader img {
  width: 120px;
  height: 120px;
  border-radius: 6px;
}
.qrcode-uploader:hover {
  border-color: #409eff;
}
.qrcode-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
</style>

